package controllers;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import models.ActionLog;
import models.EEntity;
import models.Idea;
import models.MergeRequest;
import models.Notification;
import models.Organization;
import models.Topic;
import models.User;
import play.mvc.Controller;

public class IdeaController extends Controller {

	/**
	 * Renders the idea to be merged to the Merge Request page , if there are
	 * other valid ideas that can be merged with, because draft/promoted/hidden
	 * ideas are not valid to be merged, so the method gets the number of all
	 * ideas that can be merged , if this number is zero then the method renders
	 * the idea id to the viewIdea page showing an error message
	 * "There are no other ideas in this topic to merge this one with." All of
	 * this occurs if the idea is not null, if its null then the user is
	 * redirected to a page which shows the user a message that the idea is not
	 * found.
	 * 
	 * @author Hesham Massoud
	 * 
	 * @story C3S41
	 * 
	 * @param ideaId
	 *            The id of the idea requested to be merged.
	 * 
	 * */
	public static void viewRequestMerge(long ideaId) {
		Idea ideaToBeMerged = Idea.findById(ideaId);
		notFoundIfNull(ideaToBeMerged);
		Topic topic = ideaToBeMerged.topic;
		int numberOfIdeas = 0;
		for (Idea idea : topic.ideas) {
			if (!idea.isDraft && idea != ideaToBeMerged && !idea.isPromoted
					&& !idea.isHidden) {
				numberOfIdeas++;
			}
		}
		if (numberOfIdeas == 0) {
			flash.error("There are no other ideas in this topic to merge this one with.");
			Browse.viewIdea(ideaId);
		} else {
			render(ideaToBeMerged);
		}
	}

	/**
	 * Renders the id of the idea to a page that will show the users all the
	 * videos that this idea has with the ability to remove each one or add a
	 * new video, if the idea is equal to null then the page redirects to a page
	 * which gives the user a message that the idea is not found.
	 * 
	 * @author Hesham Massoud
	 * 
	 * @story Extra
	 * 
	 * @param ideaId
	 *            The id of the idea requested to be merged.
	 * 
	 * */
	public static void viewAddVideo(long ideaId) {
		Idea idea = Idea.findById(ideaId);
		notFoundIfNull(idea);
		render(idea);
	}

	/**
	 * Creates a new merge request with its given details,sends it to the
	 * organizers of this entity and to the organization lead, sends them
	 * notifications and mails.Then shows the user a success message and
	 * redirects him to the idea page. If the idea to be merged or the second
	 * idea is equal to null, the user is redirected to a page which shows the user
	 * a message that the idea is not found.
	 *  
	 * @author Hesham Massoud
	 * 
	 * @story C3S41
	 * 
	 * @param report
	 *            It is the report given by the user to describe the details of
	 *            the merge required.
	 * @param toBeMerged
	 *            It is the id of the first idea to be merged.
	 * @param secondIdea
	 *            It is the id of the second idea to be merged with the first.
	 * */
	public static void sendRequest(String report, long toBeMerged,
			long secondIdea) {
		User user = User.getUserInfo(session.get("username"));
		if (user != null) {
			LinkedList<User> recievers = new LinkedList<User>();
			Idea idea = Idea.findById(toBeMerged);
			notFoundIfNull(idea);
			Idea ideaTwo = Idea.findById(secondIdea);
			notFoundIfNull(idea);
			Topic topic = idea.topic;
			EEntity entity = topic.entity;
			Organization organization = entity.organization;
			recievers.addAll(entity.getOrganizers());
			recievers.add(organization.getOrganizationLead());
			MergeRequest request = new MergeRequest(report, idea, ideaTwo,
					recievers, user);
			String actionMsg = user.userName
			+ " sent a merge request to the organizers of  " + entity.name
			+ " and the Organization Lead of " + organization.name 
			+ " to merge " + idea.title + " and " + ideaTwo.title + " to be merged into one idea.";
			new ActionLog(actionMsg, user, null).save();
			Notification newNotification = new Notification(
					user.userName
							+ " has sent you a request to merge two ideas :-\n1) "
							+ idea.title
							+ "\n2) "
							+ ideaTwo.title
							+ "\nHe sent you this Report along with the request:-\n"
							+ report
							+ "\nYou can accept the request and do the merge yourself or just reject the request. Both actions can be done by visiting the Merge Request page on your profile.\nThank you.",
					request.id, 13, user, recievers).save();
			Application.sendMail(newNotification.description,
					"Idea Mangement Tool : Merge Request !!", recievers);
			flash.success("Request Sent Successfully");
			Browse.viewIdea(toBeMerged);
		} else {
			render("Application/notLoggedIn.html");
		}
	}

	/**
	 * renders a list of Top featured ideas according to this users preferences.
	 * 
	 * @author Ahmed Mahmoud
	 */
	public static void getFeaturedIdeas() {
		String username = session.get("username");
		notFoundIfNull(username);
		User user = User.getUserInfo(username);
		notFoundIfNull(user);
		List<Idea> featuredIdeas = new ArrayList<Idea>();
		for (Idea idea : user.featuredIdeas) {
			if (idea.featuredIn.isSecret()) {
				if (idea.featuredIn.isMember(user.id)
						&& idea.isAllowedToBeViewedToIdeaDevelopers()) {
					featuredIdeas.add(idea);
				}
			} else if (idea.isAllowedToBeViewedToIdeaDevelopers()) {
				featuredIdeas.add(idea);
			}
		}
		boolean isIdeas = true;
		render("Application/Featured.html", featuredIdeas, isIdeas);
	}
}